#include "global.h"
#include "sdio_sdcard.h"
#include "mass_mal.h"
#include "display.h"

#if (USE_SDCARD == 1)

uint32_t Mass_Memory_Size[2];
uint32_t Mass_Block_Size[2];
uint32_t Mass_Block_Count[2];
vu32 Status = 0;

SD_CardInfo CardInfo;

/*******************************************************************************
* Function Name  : MAL_Init
* Description    : Initializes the Media on the STM32
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
uint16_t MAL_Init(uint8_t lun)
{
    uint16_t status = MAL_OK;
    //    uint32_t DeviceSizeMul, NumberOfBlocks = 0;

    switch (lun)
    {
    case 0:
        if ((Status = SD_Init()) != SD_OK)
        {
            showCriticalStatus(" SD init error [code:%d]\n  Only RESET possible at now", Status);
        }
        scr_puts("\nRead CSD/CID MSD registers..");
        if ((Status = SD_GetCardInfo(&CardInfo)) != SD_OK)
        {
            showCriticalStatus(" SD get card info\n  error [code:%d]\n  Only RESET possible at now", Status);
        }
        scr_printf("\nSD size:%d\nblock:%d type:%d", CardInfo.CardCapacity, CardInfo.CardBlockSize, CardInfo.CardType);
        //        scr_puts("\nSD select..\r\n");
        //        if ((Status = SD_SelectDeselect((uint32_t)(CardInfo.RCA << 16))) != SD_OK)
        //        {
        //            showCriticalStatus(" SD select card\n  error [code:%d]\n  Only RESET possible at now", Status);
        //        }
        //        scr_puts("\nSD select OK\r\n");
                //        if ((Status = SD_SetDeviceMode(SD_POLLING_MODE)) != SD_OK)
                //            showCriticalStatus(" SD set device mode\n  error [code:%d]\n  Only RESET possible at now", Status);

        //        DeviceSizeMul = (CardInfo.SD_csd.DeviceSizeMul + 2);
        //        if (CardInfo.CardType == SDIO_HIGH_CAPACITY_SD_CARD)
        //        {
        //            Mass_Block_Count[0] = (CardInfo.SD_csd.DeviceSize + 1) * 1024;
        //        }
        //        else
        //        {
        //            NumberOfBlocks = ((1 << (CardInfo.SD_csd.RdBlockLen)) / 512);
        //            Mass_Block_Count[0] = ((CardInfo.SD_csd.DeviceSize + 1) * (1 << DeviceSizeMul) << (NumberOfBlocks / 2));
        //        }
        //        Mass_Block_Size[0] = 512;
        //        Status = SD_EnableWideBusOperation(SDIO_BusWide_4b);
        //        Status = SD_SetDeviceMode(SD_DMA_MODE);
        break;
    default:
        return MAL_FAIL;
    }
    return status;
}

/*******************************************************************************
* Function Name  : MAL_Write
* Description    : Write sectors
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
uint16_t MAL_Write(uint8_t lun, uint32_t Memory_Offset, uint32_t* Writebuff, uint16_t Transfer_Length)
{
    switch (lun)
    {
    case 0:
        //        Status = SD_WriteBlock(Memory_Offset, Writebuff, Transfer_Length);
        //        if (Status != SD_OK)
        //            return MAL_FAIL;
        break;
    default:
        return MAL_FAIL;
    }
    return MAL_OK;
}

/*******************************************************************************
* Function Name  : MAL_Read
* Description    : Read sectors
* Input          : None
* Output         : None
* Return         : Buffer pointer
*******************************************************************************/
uint16_t MAL_Read(uint8_t lun, uint32_t Memory_Offset, uint32_t* Readbuff, uint16_t Transfer_Length)
{
    switch (lun)
    {
    case 0:
        //        Status = SD_ReadBlock(Memory_Offset, Readbuff, Transfer_Length);
        //        if (Status != SD_OK)
        //            return MAL_FAIL;
        break;
    default:
        return MAL_FAIL;
    }
    return MAL_OK;
}

/*******************************************************************************
* Function Name  : MAL_GetStatus
* Description    : Get status
* Input          : None
* Output         : None
* Return         : None
*******************************************************************************/
uint16_t MAL_GetStatus(uint8_t lun)
{
    return lun == 0 ? MAL_OK : MAL_FAIL;
}

#endif
